/*
 * pps_bom
 */
package com.pitop.assembly.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baidu.fsg.uid.UidGenerator;
import com.pitop.assembly.dao.AssemblyDAO;
import com.pitop.assembly.dao.AssemblyMaterialDAO;
import com.pitop.assembly.entity.AssemblyMaterial;
import com.pitop.assembly.entity.AssemblyMaterialState;
import com.pitop.core.base.BaseDAO;
import com.pitop.core.base.BaseSVImpl;
import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.AssemblyMaterialException;
import com.pitop.core.exceptions.BaseException;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("assemblyMaterialSV")
@Slf4j
public class AssemblyMaterialSVImpl extends BaseSVImpl<AssemblyMaterial, Long> implements AssemblyMaterialSV {

    @Autowired
    private AssemblyMaterialDAO assemblyMaterialDAO;

    @Autowired
    private AssemblyDAO assemblyDAO;
    @Resource
    private UidGenerator uidGenerator;

    @Override
    protected BaseDAO getBaseDAO() {
        return assemblyMaterialDAO;
    }

    /**
     * 保存account对象
     *
     * @param entity 实体
     * @throws BaseException
     */
    @Override
    public void save(AssemblyMaterial entity) throws BaseException {
        super.save(entity);
    }

    /**
     * 加载一个对象AssemblyMaterial
     *
     * @param id
     * @param assemblyCode 组件编码
     * @param materialCode 物料编码
     * @return AssemblyMaterial
     */
    @Override
    public AssemblyMaterial load(Long id, String assemblyCode, String materialCode) {
        if (id == null && assemblyCode == null && materialCode == null) {
            throw new AssemblyMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }

        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("assemblyCode", assemblyCode);
        param.put("materialCode", materialCode);
        return assemblyMaterialDAO.load(param);
    }

    /**
     * 加载一个对象AssemblyMaterial,(将查询关联数据)
     *
     * @param id
     * @param assemblyCode 组件编码
     * @param materialCode 物料编码
     * @return AssemblyMaterial
     */
    @Override
    public AssemblyMaterial get(Long id, String assemblyCode, String materialCode) {
        if (id == null && assemblyCode == null && materialCode == null) {
            throw new AssemblyMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }

        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("assemblyCode", assemblyCode);
        param.put("materialCode", materialCode);
        return assemblyMaterialDAO.getDetail(id, assemblyCode, materialCode);
    }

    /**
     * 加载一个对象AssemblyMaterial 通过id
     *
     * @param id
     * @return AssemblyMaterial
     */
    @Override
    public AssemblyMaterial loadById(Long id) {
        if (id == null) {
            throw new AssemblyMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return assemblyMaterialDAO.loadById(id);
    }

    /**
     * 加载一个对象AssemblyMaterial 通过assemblyCode
     *
     * @param assemblyCode 组件编码
     * @return AssemblyMaterial
     */
    @Override
    public AssemblyMaterial loadByAssemblyCode(String assemblyCode) {
        if (assemblyCode == null) {
            throw new AssemblyMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return assemblyMaterialDAO.loadByAssemblyCode(assemblyCode);
    }

    /**
     * 加载一个对象AssemblyMaterial 通过materialCode
     *
     * @param materialCode 物料编码
     * @return AssemblyMaterial
     */
    @Override
    public AssemblyMaterial loadByMaterialCode(String materialCode) {
        if (materialCode == null) {
            throw new AssemblyMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return assemblyMaterialDAO.loadByMaterialCode(materialCode);
    }

    /**
     * 加载一个对象AssemblyMaterial 通过id,(将查询关联数据)
     *
     * @param id
     * @return AssemblyMaterial
     */
    @Override
    public AssemblyMaterial getById(Long id) {
        if (id == null) {
            throw new AssemblyMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return assemblyMaterialDAO.getById(id);
    }

    /**
     * 加载一个对象AssemblyMaterial 通过assemblyCode,(将查询关联数据)
     *
     * @param assemblyCode 组件编码
     * @return AssemblyMaterial
     */
    @Override
    public AssemblyMaterial getByAssemblyCode(String assemblyCode) {
        if (assemblyCode == null) {
            throw new AssemblyMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return assemblyMaterialDAO.getByAssemblyCode(assemblyCode);
    }

    /**
     * 加载一个对象AssemblyMaterial 通过materialCode,(将查询关联数据)
     *
     * @param materialCode 物料编码
     * @return AssemblyMaterial
     */
    @Override
    public AssemblyMaterial getByMaterialCode(String materialCode) {
        if (materialCode == null) {
            throw new AssemblyMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return assemblyMaterialDAO.getByMaterialCode(materialCode);
    }

    /**
     * 根据主键id,oldStates 共同更新 AssemblyMaterial 的状态到newState状态
     *
     * @param id
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateById(Long id, AssemblyMaterialState newState, AssemblyMaterialState... oldStates) {
        if (id == null) {
            throw new AssemblyMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (newState == null) {
            throw new AssemblyMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (oldStates == null) {
            throw new AssemblyMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        assemblyMaterialDAO.updateStateById(id, new Date(), newState.name(), states);
    }

    /**
     * 根据主键assemblyCode,oldStates 共同更新 AssemblyMaterial 的状态到newState状态
     *
     * @param assemblyCode 组件编码
     * @param newState     新状态
     * @param oldStates    旧状态集合
     */
    @Override
    public void updateStateByAssemblyCode(String assemblyCode, AssemblyMaterialState newState, AssemblyMaterialState... oldStates) {
        if (assemblyCode == null) {
            throw new AssemblyMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (newState == null) {
            throw new AssemblyMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (oldStates == null) {
            throw new AssemblyMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        assemblyMaterialDAO.updateStateByAssemblyCode(assemblyCode, new Date(), newState.name(), states);
    }

    /**
     * 根据主键materialCode,oldStates 共同更新 AssemblyMaterial 的状态到newState状态
     *
     * @param materialCode 物料编码
     * @param newState     新状态
     * @param oldStates    旧状态集合
     */
    @Override
    public void updateStateByMaterialCode(String materialCode, AssemblyMaterialState newState, AssemblyMaterialState... oldStates) {
        if (materialCode == null) {
            throw new AssemblyMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (newState == null) {
            throw new AssemblyMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (oldStates == null) {
            throw new AssemblyMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        assemblyMaterialDAO.updateStateByMaterialCode(materialCode, new Date(), newState.name(), states);
    }

    /**
     * 根据主键id 更新 AssemblyMaterial 的状态到另一个状态
     *
     * @param id
     * @param state 状态
     */
    @Override
    public void updateById(Long id, AssemblyMaterialState state) {
        if (id == null) {
            throw new AssemblyMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        assemblyMaterialDAO.updateById(id, state.name(), new Date());
    }

    /**
     * 根据主键assemblyCode 更新 AssemblyMaterial 的状态到另一个状态
     *
     * @param assemblyCode 组件编码
     * @param state        状态
     */
    @Override
    public void updateByAssemblyCode(String assemblyCode, AssemblyMaterialState state) {
        if (assemblyCode == null) {
            throw new AssemblyMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        assemblyMaterialDAO.updateByAssemblyCode(assemblyCode, state.name(), new Date());
    }

    /**
     * 根据主键materialCode 更新 AssemblyMaterial 的状态到另一个状态
     *
     * @param materialCode 物料编码
     * @param state        状态
     */
    @Override
    public void updateByMaterialCode(String materialCode, AssemblyMaterialState state) {
        if (materialCode == null) {
            throw new AssemblyMaterialException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        assemblyMaterialDAO.updateByMaterialCode(materialCode, state.name(), new Date());
    }


    /**
     * 删除对象AssemblyMaterial
     *
     * @param id *
     * @param assemblyCode 组件编码
     * @param materialCode 物料编码
     * @return AssemblyMaterial
     */
    @Override
    public void delete(Long id, String assemblyCode, String materialCode) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("assemblyCode", assemblyCode);
        param.put("materialCode", materialCode);
        assemblyMaterialDAO.delete(param);
    }


    /**
     * 查询AssemblyMaterial分页
     *
     * @param assemblyMaterial 对象
     * @param offset           查询开始行
     * @param limit            查询行数
     * @return List<AssemblyMaterial>
     */
    @Override
    public List<AssemblyMaterial> list(AssemblyMaterial assemblyMaterial, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }

        Map<String, Object> map = null;
        if (assemblyMaterial != null) {
            map = JSON.parseObject(JSON.toJSONString(assemblyMaterial, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return assemblyMaterialDAO.list(map, new RowBounds(offset, limit));
    }

    @Override
    public int count(AssemblyMaterial assemblyMaterial) {
        Map<String, Object> map = null;
        if (assemblyMaterial != null) {
            map = JSON.parseObject(JSON.toJSONString(assemblyMaterial, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return assemblyMaterialDAO.count(map);
    }

    /**
     * 查询AssemblyMaterial分页
     *
     * @param id     * @param assemblyCode  组件编码* @param materialCode  物料编码
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<AssemblyMaterial>
     */
    @Override
    public List<AssemblyMaterial> list(Long id, String assemblyCode, String materialCode, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("assemblyCode", assemblyCode);
        param.put("materialCode", materialCode);
        return assemblyMaterialDAO.list(param, new RowBounds(offset, limit));
    }

    @Override
    public int count(Long id, String assemblyCode, String materialCode) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("assemblyCode", assemblyCode);
        param.put("materialCode", materialCode);
        return assemblyMaterialDAO.count(param);
    }

}
